package com.youhome.clients.controller;

import com.youhome.clients.dto.DTOClientBasicsInfo;
import com.youhome.clients.dto.DTOInvestment;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.BClient;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BClientService;
import com.youhome.service.service.BUserService;
import com.youhome.service.service.ReportService;
import io.swagger.annotations.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2018/06/27.
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户")
public class UserController {
    @Autowired
    private BClientService bClientService;
    @Autowired
    private BUserService bUserService;
    @Autowired
    private ReportService reportService;

    @ApiOperation(value = "获客户信息", notes = "获客户信息")
    @GetMapping(value = "/detail")
    public Object detail(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bClientService.selectDetail(principal));
    }

    @ApiOperation(value = "修改投资信息", notes = "修改投资信息")
    @PostMapping(value = "/update/investment")
    public Object update(@ApiParam @Validated DTOInvestment dtoInvestment, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        return new Result(ResultCode.SUCCESS, bClientService.updateInvestmentByPrimaryKey(principal, modelMapper.map(dtoInvestment, BClient.class)));
    }

    @ApiOperation(value = "修改客户头像", notes = "修改客户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "photo", value = "头像路径", required = true, paramType = "path"),
    })
    @PostMapping(value = "/update/photo")
    public Object updatePhoto(String photo, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        bClientService.updatePhoto(principal, photo);
        return new Result<>(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "修改基础信息", notes = "修改基础信息")
    @PostMapping(value = "/update/basics")
    public Object updateBasics(@ApiParam @Validated DTOClientBasicsInfo dtoClientBasicsInfo, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        BClient map = modelMapper.map(dtoClientBasicsInfo, BClient.class);
        return new Result(ResultCode.SUCCESS, bClientService.updateByBasicsPrimaryKey(principal, map));
    }

    @ApiOperation(value = "用户房产报告", notes = "用户房产报告")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", required = true, paramType = "path")
    })
    @GetMapping("report/list/{pageNumber}")
    public Object reportList(@PathVariable Integer pageNumber, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        Integer pageSize = 10;
        return new ResultPage(ResultCode.SUCCESS, reportService.selectByPage(principal,pageNumber, pageSize));
    }

    @ApiOperation(value = "报告可下载数", notes = "报告可下载数")
    @GetMapping("report/count")
    public Object reportCount() throws CustomException {
        return new Result(ResultCode.SUCCESS, bUserService.selectReportCount());
    }
}
